package com.bwie.poi;//package com.bwie;
//
//import io.jsonwebtoken.Claims;
//import io.jsonwebtoken.Jwts;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.core.annotation.Order;
//import org.springframework.data.redis.core.StringRedisTemplate;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
//import java.util.concurrent.TimeUnit;
//
///**
// * @author Lenovo
// */
//@Configuration
//@Order(1)
//public class MyGlobaFiter implements GlobalFilter {
//
//    @Autowired
//    StringRedisTemplate redisTemplate;
//
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//
////请求参数发送控制台
//        ServerHttpRequest request = exchange.getRequest();
//        ServerHttpResponse response = exchange.getResponse();
//        String url = request.getURI().getPath();
//        System.out.println("请求路径"+url);
//
////        白名单可以运行
//        if (url.contains("/loginUser")) {
//            System.out.println("登录接口白名单"+url);
//            return chain.filter(exchange);
//        }
////验证token
//        String token = request.getHeaders().getFirst("token");
//        if (token==null||token.isEmpty()) {
//            System.out.println("token为空"+url);
//            response.setStatusCode(HttpStatus.UNAUTHORIZED);
//            return response.setComplete();
//        }
//
//
//        Claims body = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
//        Integer userId = body.get("userId", Integer.class);
//        String redis="token-"+userId;
//        String redisToken = redisTemplate.opsForValue().get(redis);
//
//        if (redisToken==null||redisToken.isEmpty()) {
//            System.out.println("token过期"+url);
//            response.setStatusCode(HttpStatus.UNAUTHORIZED);
//            return response.setComplete();
//        }
//        if (!token.equals(redisToken)) {
//            System.out.println("token错误"+url);
//            response.setStatusCode(HttpStatus.UNAUTHORIZED);
//            return response.setComplete();
//        }
//
//
//        redisTemplate.expire(redis,60, TimeUnit.MINUTES);
//        System.out.println("token错误"+url);
//
//
//        return chain.filter(exchange);
//    }
//}
